package com.shuhe.common;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class MyDateUtils {


    public static String getDateString() {
        Date date = new Date();
        return String.valueOf(date.getTime());
    }

    public static String secondToDate(long second, String patten) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(second * 1000);//转换为毫秒
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(patten);
        String dateString = format.format(date);
        return dateString;
    }

    public static String getDataString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getYMD() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getNowDateByHH() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HH");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getNowDateBySimple() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    //是否大于当前时间前5分钟
    public static boolean sfdydqsjq5f(String dateStr, int curMinute) throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = formatter.parse(dateStr);
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, curMinute);
        Date date2 = beforeTime.getTime();
        return date1.after(date2);
    }

    //是否小于当前时间前5分钟
    public static boolean sfxydqsjq5f(String dateStr, int curMinute) throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = formatter.parse(dateStr);
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, curMinute);
        Date date2 = beforeTime.getTime();
        return date1.before(date2);
    }

    //前一分钟
    public static String getBeforeMinuteBydate(String dateStr, int minute) throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = formatter.parse(dateStr);
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.setTime(date);
        beforeTime.add(Calendar.MINUTE, minute);
        Date currentTime = beforeTime.getTime();

        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getBeforeDayBydate(String dateStr, int day) throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = formatter.parse(dateStr);
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.setTime(date);
        beforeTime.add(Calendar.DAY_OF_MONTH, day);
        Date currentTime = beforeTime.getTime();

        String dateString = formatter.format(currentTime);
        return dateString;
    }


    public static String getBeforeMinute(int minute) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, minute);
        Date currentTime = beforeTime.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getBeforeMinuteM(int minute) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, minute);
        Date currentTime = beforeTime.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateString = formatter.format(currentTime);
        return dateString.concat(":00");
    }

    //前面的时间大，返回负数
    public static long timeDifference(String time1Str, String time2Str) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串转换为LocalDateTime对象
        LocalDateTime dateTime1 = LocalDateTime.parse(time1Str, formatter);
        LocalDateTime dateTime2 = LocalDateTime.parse(time2Str, formatter);

        // 计算时间差
        Duration duration = Duration.between(dateTime1, dateTime2);
        long differenceInMinutes = duration.toMinutes();
        return differenceInMinutes;
    }


    public static String getAfterDay(String nyr, int day) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = formatter.parse(nyr);
            Calendar cdate = Calendar.getInstance();
            cdate.setTime(date);
            //logger.info(formatter.format(date.getTime()));
            cdate.add(Calendar.DATE, day);
            String dateString = formatter.format(cdate.getTime());
            return dateString;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static String getLong(String nyr) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = formatter.parse(nyr);
            String dateString = String.valueOf(date.getTime() * 1000).substring(0, 10);
            return dateString;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public static long getLongByInt(String nyr) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = formatter.parse(nyr);
            return date.getTime() * 1000;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 0;
    }


    public static void main(String[] args) {
        System.out.println(getLong("2024-06-10 00:00:00"));
        System.out.println(getLong("2024-08-01 00:00:00"));
        System.out.println(getLongByInt(getNowDate()));
        if (getLongByInt(getNowDate()) > 1722441600000000L) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}
